package com.xing.lei.happy.utils;

import java.util.*;

public class ArrayUtils extends org.apache.commons.lang3.ArrayUtils {
	public static String[] trim(String[] arr) {
		String[] result = null;
		if (arr != null) {
			result = new String[0];
			for (String str : arr) {
				if (StringUtils.isNotBlank(str)) {
					result = (String[]) add(result, str.trim());
				}
			}
		}
		return result;
	}



	public static String[] sort(String[] arr) {
		String[] result = null;
		if (arr != null) {
			class TextComparator implements Comparator<String> {
				public int compare(String str1, String str2) {
					return str1.compareTo(str2);
				}
			}
			List<String> list = new ArrayList<String>();
			for (String str : arr) {
				list.add(str);
			}
			Collections.sort(list, new TextComparator());
			result = list.toArray(new String[0]);
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public static List toList(Object[] objs) {
		if (objs == null) {
			return null;
		}
		List list = new ArrayList();
		if (objs.length == 0) {
			return list;
		}
		for (Object obj : objs) {
			list.add(obj);
		}
		return list;
	}


	public static int[] averageCountIndex(int num, int count) {
		if (num < 0 || count <= 0) {
			return null;
		}
		int length = num / count, remainder = num % count, index = 0;
		int[] arr = new int[count];
		for (int i = 0; i < count; i++) {
			arr[i] = index;
			index += length;
			if (remainder > 0) {
				index++;
				remainder--;
			}
		}
		return arr;
	}

	public static int[] averageLengthIndex(int num, int length) {
		if (num < 0 || length <= 0) {
			return null;
		}
		int count = num / length;
		if (num % length > 0) {
			count++;
		}
		int[] arr = new int[count];
		for (int i = 0; i < count; i++) {
			arr[i] = i * length;
		}
		return arr;
	}

	public static Object[] filter(Object... arr) {
		return filter(arr, Object.class);
	}
	public static <T> T get(T[] ts, int i) {
		if (ts == null || i < 0 || ts.length <= i) {
			return null;
		} else {
			return ts[i];
		}
	}

	public static <T> T getFirst(T[] ts) {
		return get(ts, 0);
	}

	public static <T> T getSecond(T[] ts) {
		return get(ts, 1);
	}

	public static <T> T getThird(T[] ts) {
		return get(ts, 2);
	}

	public static <T> T getLast(T[] ts) {
		return get(ts, length(ts) - 1);
	}



	public static Boolean getBoolean(Object[] arr, int i) {
		return ObjectUtils.toBoolean(get(arr, i));
	}

	public static Boolean getFirstBoolean(Object[] arr) {
		return ObjectUtils.toBoolean(getFirst(arr));
	}

	public static Boolean getSecondBoolean(Object[] arr) {
		return ObjectUtils.toBoolean(getSecond(arr));
	}

	public static Boolean getThirdBoolean(Object[] arr) {
		return ObjectUtils.toBoolean(getThird(arr));
	}

	public static Boolean getLastBoolean(Object[] arr) {
		return ObjectUtils.toBoolean(getLast(arr));
	}

	public static Short getShort(Object[] arr, int i) {
		return ObjectUtils.toShort(get(arr, i));
	}

	public static Short getFirstShort(Object[] arr) {
		return ObjectUtils.toShort(getFirst(arr));
	}

	public static Short getSecondShort(Object[] arr) {
		return ObjectUtils.toShort(getSecond(arr));
	}

	public static Short getThirdShort(Object[] arr) {
		return ObjectUtils.toShort(getThird(arr));
	}

	public static Short getLastShort(Object[] arr) {
		return ObjectUtils.toShort(getLast(arr));
	}

	public static Integer getInteger(Object[] arr, int i) {
		return ObjectUtils.toInteger(get(arr, i));
	}

	public static Integer getFirstInteger(Object[] arr) {
		return ObjectUtils.toInteger(getFirst(arr));
	}

	public static Integer getSecondInteger(Object[] arr) {
		return ObjectUtils.toInteger(getSecond(arr));
	}

	public static Integer getThirdInteger(Object[] arr) {
		return ObjectUtils.toInteger(getThird(arr));
	}

	public static Integer getLastInteger(Object[] arr) {
		return ObjectUtils.toInteger(getLast(arr));
	}

	public static Long getLong(Object[] arr, int i) {
		return ObjectUtils.toLong(get(arr, i));
	}

	public static Long getFirstLong(Object[] arr) {
		return ObjectUtils.toLong(getFirst(arr));
	}

	public static Long getSecondLong(Object[] arr) {
		return ObjectUtils.toLong(getSecond(arr));
	}

	public static Long getThirdLong(Object[] arr) {
		return ObjectUtils.toLong(getThird(arr));
	}

	public static Long getLastLong(Object[] arr) {
		return ObjectUtils.toLong(getLast(arr));
	}

	public static Float getFloat(Object[] arr, int i) {
		return ObjectUtils.toFloat(get(arr, i));
	}

	public static Float getFirstFloat(Object[] arr) {
		return ObjectUtils.toFloat(getFirst(arr));
	}

	public static Float getSecondFloat(Object[] arr) {
		return ObjectUtils.toFloat(getSecond(arr));
	}

	public static Float getThirdFloat(Object[] arr) {
		return ObjectUtils.toFloat(getThird(arr));
	}

	public static Float getLastFloat(Object[] arr) {
		return ObjectUtils.toFloat(getLast(arr));
	}

	public static Double getDouble(Object[] arr, int i) {
		return ObjectUtils.toDouble(get(arr, i));
	}

	public static Double getFirstDouble(Object[] arr) {
		return ObjectUtils.toDouble(getFirst(arr));
	}

	public static Double getSecondDouble(Object[] arr) {
		return ObjectUtils.toDouble(getSecond(arr));
	}

	public static Double getThirdDouble(Object[] arr) {
		return ObjectUtils.toDouble(getThird(arr));
	}

	public static Double getLastDouble(Object[] arr) {
		return ObjectUtils.toDouble(getLast(arr));
	}

	public static short get(short[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static short getFirst(short[] arr) {
		return get(arr, 0);
	}

	public static short getSecond(short[] arr) {
		return get(arr, 1);
	}

	public static short getThird(short[] arr) {
		return get(arr, 1);
	}

	public static short getLast(short[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static int get(int[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static int getFirst(int[] arr) {
		return get(arr, 0);
	}

	public static int getSecond(int[] arr) {
		return get(arr, 1);
	}

	public static int getThird(int[] arr) {
		return get(arr, 1);
	}

	public static int getLast(int[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static long get(long[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static long getFirst(long[] arr) {
		return get(arr, 0);
	}

	public static long getSecond(long[] arr) {
		return get(arr, 1);
	}

	public static long getThird(long[] arr) {
		return get(arr, 1);
	}

	public static long getLast(long[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static float get(float[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static float getFirst(float[] arr) {
		return get(arr, 0);
	}

	public static float getSecond(float[] arr) {
		return get(arr, 1);
	}

	public static float getThird(float[] arr) {
		return get(arr, 1);
	}

	public static float getLast(float[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static double get(double[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static double getFirst(double[] arr) {
		return get(arr, 0);
	}

	public static double getSecond(double[] arr) {
		return get(arr, 1);
	}

	public static double getThird(double[] arr) {
		return get(arr, 1);
	}

	public static double getLast(double[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static char get(char[] arr, int i) {
		if (arr == null || i < 0 || arr.length <= i) {
			return 0;
		} else {
			return arr[i];
		}
	}

	public static char getFirst(char[] arr) {
		return get(arr, 0);
	}

	public static char getSecond(char[] arr) {
		return get(arr, 1);
	}

	public static char getThird(char[] arr) {
		return get(arr, 1);
	}

	public static char getLast(char[] arr) {
		return get(arr, length(arr) - 1);
	}

	public static boolean isArray(Object obj) {
		return obj != null && obj.getClass().isArray();
	}

	public static boolean isNotArray(Object obj) {
		return !isArray(obj);
	}

	public static int length(Object[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(short[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(int[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(long[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(float[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(double[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(byte[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}

	public static int length(char[] arr) {
		return isEmpty(arr) ? 0 : arr.length;
	}








	public static String[] toStringArray(char[] chars) {
		if (chars == null) {
			return null;
		}
		int len = chars.length;
		String[] arr = new String[len];
		for (int i = 0; i < len; i++) {
			arr[i] = String.valueOf(chars[i]);
		}
		return arr;
	}






}